﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserInterface.Views
{   
    using Helpers;

    /// <summary>
    /// Interaction logic for Relationships.xaml
    /// </summary>
    public partial class Relationships : UserControl
    {
        public Relationships(RelationshipInfomation relations)
        {
            InitializeComponent();
            this.InitContent(relations);
        }

        private void InitContent(RelationshipInfomation relations)
        {
            this.ClearGridContent();
            this.SetGridColumnDefinitions(relations.matrix.GetLength(0));
            this.SetGridRowDefinitions(relations.matrix.GetLength(0));
            this.SetContents(relations);
        }

        private void ClearGridContent()
        {
            this.grid.Children.Clear();
            this.grid.RowDefinitions.Clear();
            this.grid.ColumnDefinitions.Clear();
        }

        private void SetGridRowDefinitions(int number)
        {
            for(int i=0;i<number;i++)
            {     
                RowDefinition rd = this.GetRowDefinition();
                this.grid.RowDefinitions.Add(rd);
            }
        }
        private RowDefinition GetRowDefinition()
        {
            RowDefinition rd = new RowDefinition();
            rd.Height = new GridLength(1, GridUnitType.Auto);
            return rd;
        }

        private void SetGridColumnDefinitions(int number)
        {
            for (int i = 0; i < number; i++)
            {
                ColumnDefinition cd = this.GetColumnDefinition();
                this.grid.ColumnDefinitions.Add(cd);
            }
        }

        private ColumnDefinition GetColumnDefinition()
        {
            ColumnDefinition cd = new ColumnDefinition();
            cd.Width = new GridLength(1, GridUnitType.Auto);
            return cd;
        }


        private void SetContents(RelationshipInfomation relations)
        {
           for(int x=0;x<relations.matrix.GetLength(0);x++)
               for (int y = 0; y < relations.matrix.GetLength(1); y++)
               {
                   Label l = this.GetLabelAsContent(relations.matrix[x, y]);
                   this.SetLabelInGrid(l, x, y);
               }
        }

        private Label GetLabelAsContent(string content)
        {
            Label l = new Label();
            l.Content = content;
            return l;
        }

        private void SetLabelInGrid(Label label, int x, int y)
        {
            Grid.SetColumn(label, x);
            Grid.SetRow(label, y);
            this.grid.Children.Add(label);
        }

        
    }
}
